import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export function checkPermission(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value

    const hasPermission = roles.some(role => {
      return permissionRoles.includes(role)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    return false
  }
}

/**
 * 检测按钮权限
 * @param type
 * @param pointObj
 * @returns {number}
 */
export function permissionPoints(type, pointObj) {
  if (pointObj) {
    let permission = 0;
    switch (type) {
      case 1:
        if (pointObj.addStatus == 1) {//增
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      case 2:
        if (pointObj.delStatus == 1) {//删
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      case 3:
        if (pointObj.viewStatus == 1) {//查
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      case 4:
        if (pointObj.editStatus == 1) {//改
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      case 5:
        if (pointObj.importStatus == 1) {//导入
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      case 6:
        if (pointObj.export == 1) {//导出
          permission = 1;
        } else {
          permission = 0;
        }
        break;
      default:
        permission = 0;
    }
    return permission;
  }
}
